package util;

import com.mysql.jdbc.Statement;
import com.nanosl.lib.db.Manager;
import ent.DamageNotes;
import ent.Employee;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Fix {

    static Manager manager = Manager.getInstance();

    public static void fix() {
        five();
        six();
        System.out.println("fixed");
    }

    private static void one() {
        try {
            String q1 = "ALTER TABLE `sgm`.`sr_stock` ADD COLUMN `pack_price` DOUBLE NULL  AFTER `employee_code` , ADD COLUMN `unit_price` DOUBLE NULL  AFTER `pack_price` ";
            String q2 = "UPDATE sr_stock,sr_price SET sr_stock.pack_price = sr_price.pack, sr_stock.unit_price = sr_price.unit WHERE sr_stock.item_code = sr_price.item_code AND sr_stock.employee_code =sr_price.employee_code";
            String q3 = "ALTER TABLE `sgm`.`purchase_invoice` CHANGE COLUMN `inv_time` `inv_date` DATE NULL DEFAULT NULL";
            java.sql.Connection connection = manager.getConnection();
            Statement stmt = (Statement) connection.createStatement();
            stmt.executeUpdate(q1);
            stmt.executeUpdate(q2);
            stmt.executeUpdate(q3);
        } catch (SQLException ex) {
            Logger.getLogger(Fix.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private static void two() {
        try {
            String q1 = "ALTER TABLE `sgm`.`purchase_invoice` CHANGE COLUMN `inv_time` `inv_date` DATE NULL DEFAULT NULL";
            java.sql.Connection connection = manager.getConnection();
            Statement stmt = (Statement) connection.createStatement();
            stmt.executeUpdate(q1);
        } catch (SQLException ex) {
            Logger.getLogger(Fix.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private static void five() {
        Collection<DamageNotes> damageNoteses = manager.find(DamageNotes.class);
        for (Iterator<DamageNotes> it = damageNoteses.iterator(); it.hasNext();) {
            DamageNotes damageNotes = it.next();
            Employee employee = manager.find(Employee.class, damageNotes.getReason().substring(0, 3));
            System.out.println(employee);
            damageNotes.setEmployee(employee);
            damageNotes.getDamageNotesPK().setEmployeeCode(employee.getCode());
            manager.update(damageNotes);
        }
    }
//
//    private static void three() {
//        try {
//            String q1 = "ALTER TABLE `sgm`.`operator` ADD COLUMN `created_date` DATE NULL  AFTER `created_By` , CHANGE COLUMN `createdBy` `created_By` VARCHAR(255) NULL DEFAULT NULL";
//            java.sql.Connection connection = manager.getConnection();
//            Statement stmt = (Statement) connection.createStatement();
//            stmt.executeUpdate(q1);
//        } catch (SQLException ex) {
//            Logger.getLogger(Fix.class.getName()).log(Level.SEVERE, null, ex);
//        }
//    }
//
//    private static void four() {
//        try {
//           String q2 ="ALTER TABLE `sgm`.`operator` CHANGE COLUMN `employeeCode` `employee_code` VARCHAR(255) NULL DEFAULT NULL  , DROP INDEX `employee` , ADD UNIQUE INDEX `employee` (`employee_code` ASC)";
//            java.sql.Connection connection = manager.getConnection();
//            Statement stmt = (Statement) connection.createStatement();
//            stmt.executeUpdate(q2);
//        } catch (SQLException ex) {
//            Logger.getLogger(Fix.class.getName()).log(Level.SEVERE, null, ex);
//        }
//    }

    public static void main(String[] args) {
        fix();
    }

    private static void six() {

        Collection<DamageNotes> damageNoteses = manager.find(DamageNotes.class);
        for (Iterator<DamageNotes> it = damageNoteses.iterator(); it.hasNext();) {
            DamageNotes damageNotes = it.next();
            if (damageNotes.getEmployee() == null) {
                manager.delete(DamageNotes.class, damageNotes.getDamageNotesPK());
            }
        }
    }
}
